
#include "nx_api.h"

#ifdef __PRODUCT_NETXDUO__

#include "netx_mdns_test.h"

/* Frame (75 bytes) */
static const unsigned char pkt1[75] = {
0x01, 0x00, 0x5e, 0x00, 0x00, 0xfb, 0x00, 0x11, /* ..^..... */
0x22, 0x33, 0x44, 0x57, 0x08, 0x00, 0x45, 0x00, /* "3DW..E. */
0x00, 0x3d, 0x00, 0x02, 0x40, 0x00, 0xff, 0x11, /* .=..@... */
0x90, 0x93, 0x0a, 0x00, 0x00, 0x1f, 0xe0, 0x00, /* ........ */
0x00, 0xfb, 0x14, 0xe9, 0x14, 0xe9, 0x00, 0x29, /* .......) */
0x66, 0xba, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, /* f....... */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x5f, /* ......._ */
0x69, 0x70, 0x70, 0x04, 0x5f, 0x74, 0x63, 0x70, /* ipp._tcp */
0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x00, 0x00, /* .local.. */
0x0c, 0x00, 0x01                                /* ... */
};

/* Frame (276 bytes) */
static const unsigned char pkt2[276] = {
0x01, 0x00, 0x5e, 0x00, 0x00, 0xfb, 0x00, 0x11, /* ..^..... */ 
0x22, 0x33, 0x44, 0x56, 0x08, 0x00, 0x45, 0x00, /* "3DV..E. */
0x01, 0x06, 0x00, 0x07, 0x40, 0x00, 0xff, 0x11, /* ....@... */
0xd9, 0x1c, 0xc0, 0xa8, 0x00, 0x1f, 0xe0, 0x00, /* ........ */
0x00, 0xfb, 0x14, 0xe9, 0x14, 0xe9, 0x00, 0xf2, /* ........ */
0x15, 0xc4, 0x00, 0x00, 0x84, 0x00, 0x00, 0x00, /* ........ */
0x00, 0x01, 0x00, 0x00, 0x00, 0x03, 0x04, 0x5f, /* ......._ */
0x69, 0x70, 0x70, 0x04, 0x5f, 0x74, 0x63, 0x70, /* ipp._tcp */
0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x00, 0x00, /* .local.. */
0x0c, 0x00, 0x01, 0x00, 0x00, 0x00, 0x78, 0x00, /* ......x. */
0x23, 0x11, 0x53, 0x69, 0x6d, 0x70, 0x6c, 0x65, /* #.Simple */
0x20, 0x57, 0x65, 0x62, 0x20, 0x53, 0x65, 0x72, /*  Web Ser */
0x76, 0x65, 0x72, 0x04, 0x5f, 0x69, 0x70, 0x70, /* ver._ipp */
0x04, 0x5f, 0x74, 0x63, 0x70, 0x05, 0x6c, 0x6f, /* ._tcp.lo */
0x63, 0x61, 0x6c, 0x00, 0x11, 0x53, 0x69, 0x6d, /* cal..Sim */
0x70, 0x6c, 0x65, 0x20, 0x57, 0x65, 0x62, 0x20, /* ple Web  */
0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x04, 0x5f, /* Server._ */
0x69, 0x70, 0x70, 0x04, 0x5f, 0x74, 0x63, 0x70, /* ipp._tcp */
0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x00, 0x00, /* .local.. */
0x21, 0x80, 0x01, 0x00, 0x00, 0x00, 0x78, 0x00, /* !.....x. */
0x15, 0x00, 0x00, 0x00, 0x00, 0x1f, 0x90, 0x07, /* ........ */
0x75, 0x62, 0x75, 0x6e, 0x74, 0x75, 0x31, 0x05, /* ubuntu1. */
0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x00, 0x11, 0x53, /* local..S */ 
0x69, 0x6d, 0x70, 0x6c, 0x65, 0x20, 0x57, 0x65, /* imple We */
0x62, 0x20, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, /* b Server */ 
0x04, 0x5f, 0x69, 0x70, 0x70, 0x04, 0x5f, 0x74, /* ._ipp._t */
0x63, 0x70, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, /* cp.local */
0x00, 0x00, 0x10, 0x80, 0x01, 0x00, 0x00, 0x00, /* ........ */
0x78, 0x00, 0x14, 0x08, 0x70, 0x61, 0x70, 0x65, /* x...pape */ 
0x72, 0x3d, 0x41, 0x34, 0x0a, 0x76, 0x65, 0x72, /* r=A4.ver */
0x73, 0x69, 0x6f, 0x6e, 0x3d, 0x30, 0x31, 0x07, /* sion=01. */
0x75, 0x62, 0x75, 0x6e, 0x74, 0x75, 0x31, 0x05, /* ubuntu1. */
0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x00, 0x00, 0x01, /* local... */
0x80, 0x01, 0x00, 0x00, 0x00, 0x78, 0x00, 0x04, /* .....x.. */
0xc0, 0xa8, 0x00, 0x1f                          /* ....     */
};

/* Frame (75 bytes) */
static const unsigned char pkt3[75] = {
0x01, 0x00, 0x5e, 0x00, 0x00, 0xfb, 0x00, 0x11, /* ..^..... */
0x22, 0x33, 0x44, 0x57, 0x08, 0x00, 0x45, 0x00, /* "3DW..E. */
0x00, 0x3d, 0x00, 0x02, 0x40, 0x00, 0xff, 0x11, /* .=..@... */
0x90, 0x93, 0x0a, 0x00, 0x00, 0x1f, 0xe0, 0x00, /* ........ */
0x00, 0xfb, 0x14, 0xe9, 0x14, 0xe9, 0x00, 0x29, /* .......) */
0x66, 0xba, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, /* f....... */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x5f, /* ......._ */
0x69, 0x70, 0x70, 0x04, 0x5f, 0x74, 0x63, 0x70, /* ipp._tcp */
0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x00, 0x00, /* .local.. */
0x0c, 0x00, 0x01                                /* ... */
};

static MDNS_QUERY_INFO mdns_query = {NX_NULL, "_ipp._tcp", NX_NULL};
#define resonable_error 10

MDNS_TEST_SEQ mdns_query_rr_timeout[] = {
    {TITLE, "RR timeout", 10, 0},
    
    /* Waiting for probing and announcing.  */
    {WAIT, NX_NULL, 0, 5},

    /* Add query. */
    {MDNS_QUERY, (char*)&mdns_query, 0, 0},
    {MDNS_CHECK_DATA_V4, (char*)&pkt1[0], sizeof(pkt1), 5},

    /* Inject a response. */
    {INJECT, (char*)&pkt2[0], sizeof(pkt2), 0},

    /* Reset timer. */
    {MDNS_TIMER_RESET, NX_NULL, 0, 0},

    /* TTL is 120s. */
    /* Issue a query at 80%, 85%, 90%, 95% of the record lifetime. */
    {WAIT, NX_NULL, 0, 95},
    {DUMP, NX_NULL, 0, 0},

    /* Check data.  */
    {MDNS_CHECK_DATA_V4, (char*)&pkt3[0], sizeof(pkt3), 10},

    /* Check timer in range 96~99s (80%-82%). */
    {MDNS_TIMER_CHECK, NX_NULL, 150 + resonable_error, 9750},
    {DUMP, NX_NULL, 0, 0},

    /* Check data.  */
    {MDNS_CHECK_DATA_V4, (char*)&pkt3[0], sizeof(pkt3), 10},

    /* Check timer in range 102~105 (85%-87%). */
    {MDNS_TIMER_CHECK, NX_NULL, 150 + resonable_error, 10350},
    {DUMP, NX_NULL, 0, 0},

    /* Check data.  */
    {MDNS_CHECK_DATA_V4, (char*)&pkt3[0], sizeof(pkt3), 10},

    /* Check timer in range 108~111 (90%-92%). */
    {MDNS_TIMER_CHECK, NX_NULL, 150 + resonable_error, 10950},
    {DUMP, NX_NULL, 0, 0},

    /* Check data.  */
    {MDNS_CHECK_DATA_V4, (char*)&pkt3[0], sizeof(pkt3), 10},

    /* Check timer in range 114~117 (95%-97%). */
    {MDNS_TIMER_CHECK, NX_NULL, 150 + resonable_error, 11550},

    /* The record should be deleted. */
    {WAIT, NX_NULL, 0, 7},
    {MDNS_CHECK_RR_COUNT_REMOTE, NX_NULL, 0, 0},

    /* Delete the query. */
    {MDNS_QUERY_DELETE, NX_NULL, 0, 0},
    {DUMP, NX_NULL, 0, 0},

    /* Inject a response. */
    {INJECT, (char*)&pkt2[0], sizeof(pkt2), 0},

    /* No cache maintenance is expected. */
    {MDNS_REJECT_ANY_V4, NX_NULL, 0, 120},

    /* The record should be deleted. */
    {WAIT, NX_NULL, 0, 2},
    {MDNS_CHECK_RR_COUNT_REMOTE, NX_NULL, 0, 0},
};

int mdns_query_rr_timeout_size = sizeof(mdns_query_rr_timeout) / sizeof(MDNS_TEST_SEQ);

#endif /* __PRODUCT_NETXDUO__  */

